/**
 * @Author:  renyc
 * @Date: 2022年12月15日11:13:56
 */

import ChartLayer from "../ChartLayer";
import ChartType from "../ChartType";

/**
 * @class PieChartLayer
 * 
 * @description 饼状图
 * 
 * @extends ChartLayer
 * 
 */
class PieChartLayer extends ChartLayer {
    constructor(name, options){
        super(name, options);
        this._chartType = ChartType.PIE;
    }

    setData(data){
        if(this._echarts){
            let options = this._buildEChartOptions(data);
            if(options){
                this._echarts.setOption(options);
            }
        }
    }

    _buildEChartOptions(data){
        let series = this._buildSeries(data);

        let echarts_pie_options =  {            
            GLMap: {},
            coordinateSystem: 'GLMap',
            series : series
            // series: [
            //     randomPieSeries([-86.753504, 33.01077], 15),
            //     randomPieSeries([116.853504, 39.8], 25),
            //     randomPieSeries([-99, 31.5], 30),              
            // ]
        };

        return echarts_pie_options;
    }

    _buildSeries(data){
        let series = [];

        let that = this;
        data.forEach(item => {
            let s = this._buildSerial(item);
            series.push(s);
        });        

        return series;
    }

    /**
     * 
     * @param {*} data 
     * 
     * @example 
     * data : {
     *      radius : 20,
     *      center : [116, 39],
     *      data ：[{
     *          name :  'A',
     *          value:  '20'
     *      },{
     *          name :  'B',
     *          value:  '30'
     *      },{
     *          name :  'C',
     *          value:  '30'
     *      }]
     * }
     */
    _buildSerial(data){
        let radius = 20;
        let center = data.position;
        let s = {
            type: 'pie',
            coordinateSystem: 'GLMap',
            // tooltip: {
            //   formatter: '{b}: {c} ({d}%)'
            // },
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            animationDuration: 0,
            radius : radius,
            center : center,
            data : data.data,
        };
        return s;
    }
}

export default PieChartLayer